package admindao

import (
	"wzgames/internal/admin/model"
	"wzgames/internal/gdao"
)

type AdminDao gdao.DaoSource

const sqlGetAdminByName = "select * from sys_admin where `name`=? limit 1"

func (dao *AdminDao) GetAdminByName(name string) (*model.Admin, error) {
	acc := &model.Admin{}
	err := dao.Get(acc, sqlGetAdminByName, name)
	return acc, err
}

const sqlGetAdminById = "select * from sys_admin where `adminid`=? limit 1"

func (dao *AdminDao) GetAdminById(adminId int64) (*model.Admin, error) {
	acc := &model.Admin{}
	err := dao.Get(acc, sqlGetAdminById, adminId)
	return acc, err
}

const sqlCreateAdmin = "insert into sys_admin(`adminid`,`name`,`nickname`,`avatar`,`status`,`gmsalt`,`gmpwd`,`itc`,`created`,`updated`)" +
	" values(:adminid,:name,:nickname,:avatar,:status,:gmsalt,:gmpwd,:itc,:created,:updated)"

func (dao *AdminDao) CreateAdmin(admin *model.Admin) error {
	_, err := dao.NamedExec(sqlCreateAdmin, admin)
	return err
}

const sqlChangePwd = "update sys_admin set `gmpwd`=:gmpwd,`gmsalt`=:gmsalt,`itc`=:itc,`updated`=:updated where `adminid`=:adminid"

func (dao *AdminDao) ChangePwd(admin *model.Admin) (int64, error) {
	result, err := dao.NamedExec(sqlChangePwd, admin)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeNickAvatar = "update sys_admin set `nickname`=:nickname,`avatar`=:avatar,`updated`=:updated where `adminid`=:adminid"

func (dao *AdminDao) ChangeNickAvatar(admin *model.Admin) (int64, error) {
	result, err := dao.NamedExec(sqlChangeNickAvatar, admin)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}

const sqlChangeStatus = "update sys_admin set `status`=:status,`updated`=:updated where `adminid`=:adminid"

func (dao *AdminDao) ChangeStatus(admin *model.Admin) (int64, error) {
	result, err := dao.NamedExec(sqlChangeStatus, admin)
	if err != nil {
		//logger.Error("err == ", zap.String("error", err.Error()))
		return 0, err
	}
	return result.RowsAffected()
}
